
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Windows;
using System.Windows.Input;
using AdventureWorks.Client.Objects;
using AdventureWorks.Services;
using Xomega.Framework;

namespace AdventureWorks.Client.WPF
{
    public partial class EmployeeSearch : Window
    {
        private EmployeeCriteria criteria;
        private DataObjectList<EmployeeRowObject> list;

        public EmployeeSearch()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            criteria = new EmployeeCriteria();
            list = new DataObjectList<EmployeeRowObject>();
            list.Editable = false;
            pnlCriteria.DataContext = criteria;
            gridResults.ItemsSource = list;
            PostInitialize();
        }

        partial void PostInitialize();

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            criteria.Validate(true);
            ErrorList valErr = criteria.GetValidationErrors();
            if (valErr.HasErrors())
            {
                Errors.Show(valErr);
                return;
            }
            ChannelFactory<IEmployeeService> factory = new ChannelFactory<IEmployeeService>("IEmployeeService");
            IEmployeeService svc = factory.CreateChannel();
            Employee_ReadListInput input = new Employee_ReadListInput();
            criteria.ToDataContract(input);
            List<Employee_ReadListOutput> output = svc.ReadList(input);
            list.FromDataContract(output);
            factory.Close();
        }
        
        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            list.Clear();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
